package com.flipdf2.serialize.ppml.element;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;

public class PPMLTicket {
	public static final String NAME = "TICKET";
	
	private PPMLInternalData internalData;
	private PPMLExternalData externalData;
	
	private String format;
  
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (externalData != null)
			root.addContent(externalData.toXML(defaultNamespace));
		
		if (internalData != null)
			root.addContent(internalData.toXML(defaultNamespace));
		
		if (format != null) 
			root.setAttribute("Format", format);
		
		return root;
	}

	public void check() throws PPMLException {
		if (format == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Format");
		}
		if (internalData == null && externalData == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ELEMENT_LOST, "(EXTERNAL_DATA | INTERNAL_DATA)");
		}
	}
	
	public PPMLExternalData getExternalData() {
		return externalData;
	}

	public void setExternalData(PPMLExternalData externalData) {
		this.externalData = externalData;
	}

	public String getFormat() {
		return format;
	}

	public void setFormat(String format) {
		this.format = format;
	}

	public PPMLInternalData getInternalData() {
		return internalData;
	}

	public void setInternalData(PPMLInternalData internalData) {
		this.internalData = internalData;
	}
}
